package Markers;

import interfaces.IMap;

public class BinMap implements IMap
{
	int Height;    
    int Width;
    
    int blockHeight;    
    int blockWidth;
    int Step = 32;
    
    int blockMap[][][];

  //===========================================================================
    public BinMap(int h, int w)
    {
    	Height = h;
    	Width = w;
    	
    	int blockHeight = Height/Step;
    	if(Height%Step!=0)
    		blockHeight++;
    	
    	int blockWidth = Width/Step;
    	if(Width%Step!=0)
    		blockWidth++;
    	
    	blockMap = new int[blockWidth][blockHeight][];    	
    }
  //===========================================================================
	@Override
	public int Height() 
	{
		return Height;
	}
	//===========================================================================
	@Override
	public int Width() 
	{
		return Width;
	}
	//===========================================================================
	@Override
	public int Get(int x, int y) 
	{
		 if (y < 0 || y >= Height)
             return 128;
         if (x < 0 || x >= Width)
             return 128;
         return getBin(x,y);
	}
	//===========================================================================
	private int getBin(int x, int y) 
	{
		int blockX = x/Step;
		int blockY = y/Step;
		return getValueInBlockMap(x%Step,y%Step,blockX,blockY);
	}
	//===========================================================================
	private int getValueInBlockMap(int inX, int inY, int blockX, int blockY) 
	{
		int[] Block = blockMap[blockX][blockY];
		
		if(Block == null)		
			return 0;
		
		if(((Block[inY]>>inX) & 1) != 0)
			return 255;			
		return 0;
	}
	//===========================================================================
	@Override
	public void Set(int x, int y, int value)
	{
		int blockX = x/Step;
		int blockY = y/Step;
		setValueInBlockMap(x%Step,y%Step,blockX,blockY,value);
	}
	//===========================================================================
	private void setValueInBlockMap(int inX, int inY, int blockX, int blockY,int value) 
	{
		int[] Block = blockMap[blockX][blockY];
		if(Block == null)
		{
			Block = new int[32];
			blockMap[blockX][blockY] = Block;
		}
		if(value>0)
			Block[inY] = Block[inY] | (1 << inX);	
		else 
			Block[inY] = Block[inY] & (~( 1 << inX));
					
	}
	//===========================================================================
}
